﻿using Autofac;
using CSRedis;
using SH.Common;
using SH.Common.CommonReturn;
using SH.Common.DataDictionary;
using SH.Multitenant.Application.IServices.IDic;
using SH.Multitenant.Infrastructure.IRepository;
using SH.Multitenant.Infrastructure.Repository;
using SqlSugar;

using System.Linq.Expressions;



namespace SH.Multitenant.Application.Services
{
    public class DicServices : IDicServices
    {
        private readonly IDicRepository _dicRepository;
        private readonly CSRedisClient _redis = AppService.AutofacContainer.Resolve<CSRedisClient>();
        public DicServices(IDicRepository dicRepository)
        {
            _dicRepository = dicRepository;
        }
        public ISugarQueryable<Dic> All => _dicRepository.All;
        //数据字典增加
        public async Task<int> DicAddAsync(Dic dic)
        {
            return await _dicRepository.DicAddAsync(dic);

        }
        //数据字典删除
        public async Task<int> DicDeleteAsync(Dic dic)
        {
            return await _dicRepository.DicDeleteAsync(dic);
        }
        //数据字典修改
        public async Task<int> DicUpdAsync(Dic dic)
        {
            return await _dicRepository.DicUpdAsync(dic);
        }
        //数据字典批量增加
        public async Task<int> DicBatchInsertAsync(List<Dic> list)
        {
            return await _dicRepository.DicBatchInsertAsync(list);
        }
        //数据字典批量删除
        public async Task<int> BatchDicsDelAsync(List<Dic> list)
        {
            return await _dicRepository.BatchDicsDelAsync(list);
        }
        //分页获取字典列表
        public async Task<ApiPaging<List<Dic>>> PagingDicsListAsync(string? identification, int pageSize, int pageIndex)
        {
            List<Dic> list = _redis.Get<List<Dic>>("dicList");
            ApiPaging<List<Dic>> apiPaging = new ApiPaging<List<Dic>>();
            if (list == null || list.Count == 0)
            {
                Expression<Func<Dic, bool>> predicate = x => true;
                if (!string.IsNullOrEmpty(identification))
                {
                    predicate = x => x.Identification.Contains(identification);
                }
                var query = All.Where(predicate).OrderBy("Id desc,Identification asc");
                int totalCount = query.Count();
                int pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

                apiPaging.TotalCount = totalCount;
                apiPaging.PageCount = pageCount;
                apiPaging.Data = list;
                return apiPaging;
            }
            else
            {

                if (!string.IsNullOrEmpty(identification))
                {
                    list = list.Where(x => x.Identification.Contains(identification)).ToList();
                }
                int totalCount = list.Count();
                int pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = list.OrderByDescending(x => x.Id).ThenBy(x => x.Identification).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                apiPaging.TotalCount = totalCount;
                apiPaging.PageCount = pageCount;
                apiPaging.Data = list;
                return apiPaging;
            }
            //var list = _redis.Get<List<Dic>>("dicList");
            //if (list == null || list.Count() == 0)
            //{


            //_redis.Set("dicList", list);
            //}


        }

        //根据唯一标识获取字典实体
        public async Task<Dic> GetDicByIdAsync(int id)
        {
            return await _dicRepository.GetDicByIdAsync(id);
        }

        //根据标识列获取字典列表
        public async Task<List<Dic>> GetDicByIdentificationAsync(string identification)
        {
            return await _dicRepository.GetDicByIdentificationAsync(identification);
        }

        //获取省市级联列表
        public List<Cascader> GetCascaderList()
        {
            return _dicRepository.GetCascaderList();
        }
    }
}
